﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.Role;
using Harmontronics.Model.Dto.User;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Asn1.Cms;
using System.Data;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        public RoleController(IRoleService role)
        {
            _roleService = role;
        }
        /// <summary>
        /// 获取全部权限
        /// </summary>
        /// <param name="getData"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResponseData GetRoleData(RoleGet getData)
        {
            List<RoleReq> roles = _roleService.GetAllRoles();
            if (getData.roleName.Length > 0)
            {
                roles = roles.Where(i => i.RoleName.Contains(getData.roleName)).ToList();
            }
            int start = (getData.page - 1) * getData.size;
            int end = ((start + getData.size) >= (roles.Count)) ? (roles.Count) : (start + getData.size);
            List<RoleReq> showroles = roles.GetRange(start, end - start);
            return ResultHelper.Success(new ReturnData() { total = roles.Count, dataList = showroles });
        }
         
        [HttpGet]
        public ApiResponseData GetRoleListAndSingle(int id)
        {
            var userRole = _roleService.GetRelationByUserId(id);
            var result = new
            {
                roles = _roleService.GetAliveRoles(),
                roleIds = userRole == null? -1:userRole.RoleId
            };
            return ResultHelper.Success(result);
        }
        [HttpGet]
        public ApiResponseData GetRoleList()
        {
            List<RoleReq> roles = _roleService.GetAliveRoles();
            return ResultHelper.Success(roles);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResponseData AddRole(RoleAdd req)
        {
            if (_roleService.Add(req))
            {
                return ResultHelper.Success("");
            }

            return ResultHelper.Error(500, "Add Failure");
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResponseData UpdateRoleData(RoleEdit req)
        {
            //获取当前登录人信息

            if (_roleService.UpdateRole(req))
            {
                return ResultHelper.Success("");
            }
            return ResultHelper.Error(500, "UpdateRole Failure");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResponseData DeleteRole(DeleteIntId deleteId)
        {
            if (_roleService.Del(deleteId.id))
            {
                return ResultHelper.Success("", "Delete Success");
            }
            //获取当前登录人信息 
            return ResultHelper.Error(500, "Delete Failure");
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResponseData BatchDelRole(List<int> ids)
        {
            if (_roleService.BatchDel(ids))
            {
                return ResultHelper.Success("", "Delete Success");
            }
            return ResultHelper.Error(500, "Delete Failure");
        }
        [HttpPost]
        public ApiResponseData UpdataRoleMenuScope(UpdateRoleMenuInput input)
        {
            if (_roleService.UpdateRoleMenus(input))
            {
                return ResultHelper.Success("", "Operate Success");
            }
            return ResultHelper.Error(500, "Operate Failure");
        }
        [HttpPost]
        public ApiResponseData ChangeRoleEnable(UpdateRoleEnable update)
        {
            if (_roleService.UpdateRoleEnable(update))
            {
                return ResultHelper.Success("", "Operate Success");
            }
            return ResultHelper.Error(500, "Operate Failure");
        }
        
    }
}
